<style lang="less" scoped>
    @import "../../less/_variable";
    .main_allScroll {
        background-color: #333;
    }
    .gift_icon{
        display: inline-block;
        width: 0.3rem;
        height: .36rem;
        background-image: url("../../assets/img/qixiGift_icon.png");
        background-size: 100% 100%;
        background-repeat: no-repeat;
        transform: translateY(3px);
    }
    .bg{
        width: 100%;
        height: 13.68rem;
        background-image: url("../../assets/img/nationalDay_c_bg.png");
        background-size: 100% auto;
        background-repeat: no-repeat;
        padding: 4.72rem .24rem .4rem;
    }
    .box{
        width: 6.8rem;
        height: 6.39rem;
        background-image: url("../../assets/img/nationalDay_Box_bg.png");
        background-size: 100% auto;
        background-repeat: no-repeat;
        position: relative;
        margin: 0.2rem auto 0;
        .box_btn{
            font-size: .28rem;
            color: #fff;
            height: .7rem;
            line-height: .7rem;
            text-align: center;
            width: 50%;
            margin: 0 auto;
        }
        .frequency{
            width: 100%;
            text-align: center;
            font-size: .28rem;
            color: #fff;
            position: absolute;
            top: 1.28rem;
            .active{
                font-size: .42rem;
                color: #FFEB41;
            }
        }
        .content{
            width: 100%;
            padding: 0 .64rem;
            position: absolute;
            top: .57rem;

        }
    }
    .text_box{
        color: #fff;
        font-size: .28rem;
        margin-top: .14rem ;
        .text_title{
            width: 2.14rem;
            height: .66rem;
            background-image: url("../../assets/img/qixiText_bg.png");
            background-size: 100% auto;
            background-repeat: no-repeat;
            text-align: center;
            line-height: .52rem;
            margin: 0 auto .16rem;
        }
        .text_content{
            font-size: .24rem;
            text-align: center;
            line-height: 1.4;
            padding: 0 .3rem;
        }
    }
    .content {
        /*padding: 0.4rem;*/
        ul {
            display: flex;
            flex-wrap: wrap;
            li {
                width: 33.33%;
                background: #FFD200;
                border: 1px solid #5700AA;
                border-radius: .2rem;
                padding-bottom: 0.08rem;
                position: relative;
                .small_box{
                    width: 100%;
                    background: #FFEB41;
                    border-radius: .2rem;
                    padding: 0.1rem 0;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    img{
                        width: 98%;
                    }
                    p{
                        font-size: .24rem;
                        color: #141306;
                    }
                }
                &.active {
                    background: #d4c541;
                    .small_box{
                        background: #d4c541;
                    }
                }
                &:nth-child(5){
                    background-color: transparent;
                    border: none;
                    .small_box{
                        background-color: transparent;
                        p{
                            font-size: 0.44rem;
                            color: #fff;
                            padding: .32rem .3rem 0;
                            text-align: center;
                            background-color: transparent;
                        }
                    }
                }
            }
        }
    }
    .btn_bg{
        width: 3.15rem;
        height: 2.9rem;
        background-image: url("../../assets/img/nationalDay_Box_btnBg.png");
        background-size: 100% auto;
        background-repeat: no-repeat;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        right: 0;
        margin: auto;
        z-index: 1;
        text-align: center;
        font-size: 0.44rem;
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        p{
            padding: .3rem .4rem;
        }
    }
    .v-transfer-dom > .vux-popup-dialog.vux-popup-top {
        width: 80.53%;
        position: absolute;
        left: 9.73%;
        top: 15%;
        border-radius: 0.2rem;
        background: #fff;

        .vux-scrollable {

            .show_header_title {

                .title {
                    margin: 0.42rem 0;
                    font-size: 0.34rem;
                    color: #333;
                    text-align: center;
                }

                span {
                    width: 0.3rem;
                    height: 0.3rem;
                    background: url('../../assets/img/close.png') no-repeat center center;
                    background-size: 100% 100%;
                    position: absolute;
                    top: 0.3rem;
                    right: 0.3rem;
                }

            }
            .show_list {
                width: 100%;
                height: 8rem;
                overflow: hidden;
                border-top: 1px solid #efefef;
                position: relative;
                .list_cont {
                    display: flex;
                    padding-left: .16rem;
                    .fight_left {
                        width: 100%;
                        display: flex;
                        flex-direction: row;
                        flex-wrap: nowrap;
                        padding: .12rem 0 .06rem;
                        border-bottom: 1px solid #f5f5f5;
                        &:last-child{
                            border: none;
                        }
                        img {
                            width: 1.1rem;
                            height: 1.1rem;
                        }

                        .list_time {
                            margin-top: 0.2rem;
                            margin-left: 0.16rem;
                            .name {
                                color: #222;
                                font-size: .28rem;
                                margin-bottom: 0.16rem;
                            }

                            .time {
                                color: #909090;
                                font-size: .24rem;
                            }

                        }
                    }
                }
            }
            .show_bottom{
                border-top: 1px solid #efefef;
                padding: .42rem 0.2rem .54rem;
                text-align: center;
                font-size: .24rem;
                line-height: 1.5;
                .select{
                    color: @c_lordColor;
                }
            }
        }
    }
</style>
<template>
    <div>
        <v-header :title="title">
            <div class="backs_icon" slot="left"></div>
        </v-header>
        <div class="main_allScroll">
            <div class="bg">
                <div class="box_btn" v-on:click="popupShow" style="display: flex;justify-content: center;align-items: center;color:#fff;">
                    <span class="gift_icon" style="margin-right: .1rem;transform: translateY(-.01rem)"></span>
                    <span>查看我的奖品</span>
                </div>
                <div class="box">
                    <!--<div class="frequency"><span>剩余抽奖次数：</span><span class="active">{{frequency}}</span></div>-->
                    <div class="content">
                        <ul>
                            <li v-for="(v,i) in list" :class="activety == v.prizesId ? 'active' : ''">
                                <div class="small_box">
                                    <img v-if="v.mainImageUrl" :src="v.mainImageUrl" alt="">
                                    <p v-if="v.prizesId != '42'">{{v.prizesName }}</p>
                                </div>
                                <!--{{v.text}}-->
                            </li>
                        </ul>
                        <div class="btn_bg">
                            <p v-on:click="start" style="opacity: 0">立即<br>抽奖</p>
                        </div>
                    </div>
                </div>
                <div class="text_box">
                    <div class="text_title">活动规则</div>
                    <div class="text_content">凡铺连铺新用户注册后即可参与一次新人抽奖活动，参与抽奖就有机会获得：开店资格、宝石券、优先券、满减券、华为P20 PRO手机。</div>
                </div>
            </div>
        </div>
        <div v-transfer-dom>
            <popup v-model="show" position="top" should-scroll-top-on-show>
                <div scrollable class="vux-scrollable">
                    <div class="show_header_title">
                        <div class="title">领奖记录</div>
                        <span @click="show = false"></span>
                    </div>
                    <div class="show_list">
                        <scroller :on-infinite="loadMore ? infinite : getDone" ref="qixiScroller">
                        <div class="list_cont" v-for="open in prizesList">
                            <div class="fight_left">
                                <img v-lazy="{src:open.mainImageUrl}" alt="">
                                <div class="list_time">
                                    <p class="name">{{open.prizesName}}</p>
                                    <p class="time">{{open.confirmTime}}</p>
                                </div>
                            </div>
                        </div>
                            <load-more v-show="!loadMore" :show-loading="false" tip="我是有底线的" background-color="#fbf9fe"></load-more>
                        </scroller>
                    </div>
                    <div class="show_bottom" v-if="!address ||  address == null">
                        <span class="select" v-on:click="gotoPage">选择收货地址</span>
                    </div>
                    <div class="show_bottom" v-else>
                        <span>当前收货地址：{{address.recipientsProvince + address.recipientsCity +  address.recipientsDistrict + address.recipientsDetailAddress }}</span><br>
                        <span class="select" v-on:click="gotoPage">修改收货地址</span>
                    </div>
                </div>
            </popup>
        </div>
    </div>
</template>
<script>
    import { TransferDom,Popup,LoadMore} from 'vux'
    import {mapActions, mapState} from 'vuex'
    import {GET_ADDRESS, SELECT_ADDRESS,UPDATESELECTFROM} from 'store/address'
    export default {
        directives: {
            TransferDom
        },
        components: {
            Popup,
            LoadMore
        },
        name: 'qixi',
        data() {
            return {
                show:false,
                activety: -2,  //当前转到哪个位置
                speed: 200, //初始化转动速度
                turns: 0, //转动次数
                cycle: 50, //最大转动次数
                timer: null,
                prizesId: -1,
                click: false,
                list: [
                    {
                        prizesName: '华为P20 pro',
                        mainImageUrl: require('../../assets/img/nationalDay_lottery_huawei.png'),
                        prizesId: 43
                    },
                    {
                        prizesName: '开店资格',
                        mainImageUrl: require('../../assets/img/nationalDay_lottery_kaidain.png'),
                        prizesId: 44},
                    {
                        prizesName: '10元满减券',
                        mainImageUrl: require('../../assets/img/nationalDay_lottery_10.png'),
                        prizesId: 45},
                    {
                        prizesName: '30元满减券',
                        mainImageUrl: require('../../assets/img/nationalDay_lottery_30.png'),
                        prizesId: 50},
                    {
                        prizesName: '立即抽奖',
                        prizesId: 42},
                    {
                        prizesName: '优先券',
                        mainImageUrl: require('../../assets/img/nationalDay_lottery_youxian.png'),
                        prizesId: 46},
                    {
                        prizesName: '宝石券',
                        mainImageUrl: require('../../assets/img/nationalDay_lottery_baoshi.png'),
                        prizesId: 49},
                    {
                        prizesName: '开店资格',
                        mainImageUrl: require('../../assets/img/nationalDay_lottery_kaidain.png'),
                        prizesId: 48},
                    {
                        prizesName: '5元满减券',
                        mainImageUrl: require('../../assets/img/nationalDay_lottery_5.png'),
                        prizesId: 47},
                ],
                prizesList:[],
                frequency: 0,
                loadMore:false,
                pageNum:1,
                title:''
            }
        },
        activated () {
            this.title = this.$route.query.t
            this.Interactive.customStatistics({type: 2, eventID: 'activity_area',map: {name: this.title}})
            this.prizesList = []
            this.frequency = 0
            this.show=false
            this.activety= 42
            this.speed= 200 //初始化转动速度
            this.turns= 0 //转动次数
            this.cycle= 50 //最大转动次数
            this.timer= null
            this.prizesId= -1
            this.click= false
            this.loadMore= false
            this.recordId = -1
            this.prizesName = ''
          if(this.comJS.getCookie('t')){
              // this.getaddressList()
              this.queryMyPrizesDeliveryId()
          }
          this.getQueryPrizesAndLotteryNumber()
        },

        deactivated(){
          // this.show = true
        },
        methods: {
            ...mapActions([GET_ADDRESS, SELECT_ADDRESS,UPDATESELECTFROM]),
            queryMyPrizesDeliveryId () {
                this.$ajax.activity.queryMyPrizesDeliveryId({activityId:'ACTIVITY_20181001_TURN_WHEEL'})
                    .then((res)=>{
                        this.address = res.body.content
                    })
            },
            prizesRecipientsInfo (data){
                this.$ajax.activity.prizesRecipientsInfo({deliveryId:data.deliveryId,activityId:'ACTIVITY_20181001_TURN_WHEEL'})
                    .then((res)=>{
                        this.address = data
                        this.UPDATESELECTFROM(null)
                    })
            },
            getQueryPrizesAndLotteryNumber () {
                this.$ajax.activity.queryPrizesAndLotteryNumber({activityId:'ACTIVITY_20181001_TURN_WHEEL'})
                    .then((res)=>{
                        this.frequency = res.body.content.lotteryNumber && res.body.content.lotteryNumber > 0 ? res.body.content.lotteryNumber : 0
                        res.body.content.list.splice(4,0,{
                            prizesName: '立即抽奖',
                            prizesId: -1
                        })
                        this.list = res.body.content.list
                    })
            },
            queryMyPrizes (done) {
                this.$ajax.activity.myPrizes({
                  activityId:'ACTIVITY_20181001_TURN_WHEEL',
                    pageNum : this.pageNum,
                    pageSize:15,
                    confirmStatus: 1
                })
                    .then((res)=>{
                        if(done){
                            done()
                        }
                        if(res.body.content.list && res.body.content.list != null){
                            if (this.pageNum == 1){
                                this.prizesList = res.body.content.list
                            }else{
                                this.prizesList = this.prizesList.concat(res.body.content.list)
                            }
                            if(res.body.content.list.length >= 15){
                                this.pageNum++
                                this.loadMore = true
                            }else{
                                this.loadMore= false
                            }
                        }
                    })
            },
            refresh(done){
              global.myLoad = true
              this.pageNum = 1
              this.prizesList = []
              this.loadMore = false
              this.$refs.qixiScroller.scrollTo(0,this.scrollPos, false)
              this.queryMyPrizes(done)
            },
            infinite(done) {
                global.myLoad = true
                this.$refs.qixiScroller.resize()
                this.loadMore = false
                this.queryMyPrizes(done)
            },
            getDone(done) {
                this.$refs.qixiScroller.finishInfinite(2);
            },
            turnActiveWheel () {
                this.$ajax.activity.turnActiveWheel({activityId:'ACTIVITY_20181001_TURN_WHEEL'})
                    .then((res)=>{
                        this.click = true
                        this.recordId = res.body.content.recordId
                        this.prizesId = res.body.content.prizesId
                        this.prizesName = res.body.content.prizesName
                        this.run()
                    })
            },
            affirmPrizes () {
                this.$ajax.activity.affirmPrizes({recordId:this.recordId,prizesId:this.activety,activityId:'ACTIVITY_20181001_TURN_WHEEL'})
                    .then((res)=>{
                        this.$vux.alert.show({
                            title:'恭喜您',
                            content:'您已抽中' + this.prizesName
                        })
                        this.frequency = res.body.content.lotteryNumber && res.body.content.lotteryNumber > 0 ? res.body.content.lotteryNumber : 0
                    })
            },
            gotoPage () {
                if (!this.comJS.getCookie('t')) {
                    this.Interactive.pushTokentoJS();
                    return
                }
              this.UPDATESELECTFROM('activety')
              this.$router.push('/my/selectAddress')
            },
            popupShow () {
                if (!this.comJS.getCookie('t')) {
                    this.Interactive.pushTokentoJS();
                    return
                }
              this.show = true
              this.pageNum = 1
              this.loadMore = false
              this.prizesList = []
              this.queryMyPrizes()
              // this.getaddressList()
            },
            start() {
                if (!this.comJS.getCookie('t')) {
                    this.Interactive.pushTokentoJS();
                    return
                }
                this.$ajax.activity.queryPrizesAndLotteryNumber({activityId:'ACTIVITY_20181001_TURN_WHEEL'})
                  .then(res =>{
                    this.frequency = res.body.content.lotteryNumber
                    if(res.body.content.activityStatus == 0){
                      this.$vux.alert.show({
                        title:'操作提示',
                        content:'活动未开始',
                      })
                      return
                    }
                    if(res.body.content.activityStatus == 2){
                      this.$vux.alert.show({
                        title:'操作提示',
                        content:'活动已结束',
                      })
                      return
                    }
                    if(res.body.content.activityStatus == 1){
                      if(!this.address ||this.address == null){
                        let that = this;
                        this.$vux.confirm.show({
                          title:'温馨提示',
                          content:'您还未设置收货地址，是否前去设置',
                          confirmText:'设置',
                          onConfirm(){
                            that.UPDATESELECTFROM('activety')
                            that.$router.push('/my/selectAddress')
                          }
                        })
                        return
                      }
                      if(this.frequency <= 0){
                        this.$vux.alert.show({
                          title:'温馨提示',
                          content:'您当前没有参与资格'
                        })
                        return
                      }
                      if (this.click) {
                        return
                      }
                      // this.activety = 0
                      this.speed = 100;
                      this.turnActiveWheel()
                    }else{
                      this.$vux.alert.show({
                        title:'操作提示',
                        content:'活动不在进行中',
                      })
                    }
                  })


            },
            stop(index) {
                this.prizesId = index;
            },
            roll() {
                let index = this.activety;
                let count = this.list.length + 41;
                index += 1;
                if (index > count) {
                    index = 43;
                }
                ;
                this.activety = index;
            },
            run() {
                this.turns += 1;
                this.roll();
                if (this.turns > this.cycle + 10 && this.prizesId == this.activety) {
                    console.log(this.prizesId, this.activety, '----' + this.turns)
                    clearTimeout(this.timer);
                    this.turns = 0;
                    this.frequency--
                    this.click = false;
                    this.prizesId = -1;
                    this.affirmPrizes()
                } else {
                    if (this.turns < this.cycle) {
                        this.speed -= 10;
                    } else if(this.prizesId > 0) {
                        if (this.turns > this.cycle + 10 && ((this.prizesId == 42 && this.activety == 50) || this.prizesId == this.activety + 1)) {
                            this.speed += 110;
                        } else {
                            this.speed += 20;
                        }
                    }
                    if (this.speed < 40) {
                        this.speed = 40;
                    }
                    this.timer = setTimeout(
                      ()=>{
                        if(this.show == false){
                          this.run()
                        }
                      }
                      , this.speed);
                }
            },
        },
        watch:{
            'interactive.refreshUserInfo':function () {
                if(this.interactive.refreshUserInfo.flang){
                    this.getQueryPrizesAndLotteryNumber();
                }
            },
            'addressList.selectAddress':function () {
              if(this.addressList.selectAddress != {} && this.addressList.selectAddress.deliveryId && this.addressList.selectForm == 'activety'){
                this.prizesRecipientsInfo(this.addressList.selectAddress)
              }
            }
        },
        computed:mapState({addressList: state => state.address,})
    }
</script>